local U = require "packages/utility/utility"

local s15__qingzheng = fk.CreateSkill {
  name = "s15__qingzheng",
}

Fk:loadTranslationTable {
  ["s15__qingzheng"] = "清正",
  [":s15__qingzheng"] = "出牌阶段开始时，你可以选择一名有手牌的其他角色，你须弃置手牌中一种花色的所有牌（至少一张），观看该角色的手牌并弃置其中一种花色的所有牌（至少一张），若弃置该角色的牌少于弃置你的牌，你对其造成1点伤害。",

  ["#s15__qingzheng-target"] = "清正：选择一名有手牌的其他角色",
  ["#s15__qingzheng-suit"] = "清正：选择一种花色弃置所有该花色的手牌",
  ["#s15__qingzheng-target-suit"] = "清正：选择一种花色弃置所有该花色手牌",
}

s15__qingzheng:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and
        player:hasSkill(s15__qingzheng.name) and
        player.phase == Player.Play and
        not player:isKongcheng() and
        table.find(player.room:getOtherPlayers(player, false), function(p)
          return not p:isKongcheng()
        end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function(p)
      return not p:isKongcheng()
    end)

    if #targets == 0 then return false end

    -- Choose target
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
      "#s15__qingzheng-target", s15__qingzheng.name, true)
    if #to == 0 then return false end

    -- Choose suit for player's own cards
    local suitNames = { "log_spade", "log_club", "log_heart", "log_diamond" }
    local suitCards = { {}, {}, {}, {} }

    for _, id in ipairs(player.player_cards[Player.Hand]) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit and not player:prohibitDiscard(id) then
        table.insertIfNeed(suitCards[suit], id)
      end
    end

    -- Check if player has any valid suits to discard
    local validSuits = {}
    for i = 1, 4 do
      if #suitCards[i] > 0 then
        table.insert(validSuits, i)
      end
    end

    if #validSuits == 0 then return false end

    local suitChoice = U.askForChooseCardList(room, player, suitNames, suitCards, 1, 1,
      s15__qingzheng.name, "#s15__qingzheng-suit", true)
    if #suitChoice == 0 then return false end

    self.cost_data = { to[1], suitChoice[1] }
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targetId = self.cost_data[1]
    local suitChoice = self.cost_data[2]
    local to = room:getPlayerById(targetId)

    -- Discard player's cards of chosen suit
    local myDiscard = table.filter(player.player_cards[Player.Hand], function(id)
      local card = Fk:getCardById(id)
      return not player:prohibitDiscard(id) and card:getSuitString(true) == suitChoice
    end)

    room:throwCard(myDiscard, s15__qingzheng.name, player, player)
    if player.dead then return end

    -- Choose suit for target's cards
    local targetSuitNames = { "log_spade", "log_club", "log_heart", "log_diamond" }
    local targetSuitCards = { {}, {}, {}, {} }

    for _, id in ipairs(to.player_cards[Player.Hand]) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit then
        table.insertIfNeed(targetSuitCards[suit], id)
      end
    end

    -- Check if target has any valid suits
    local targetValidSuits = {}
    for i = 1, 4 do
      if #targetSuitCards[i] > 0 then
        table.insert(targetValidSuits, i)
      end
    end

    local targetDiscard = {}
    if #targetValidSuits > 0 then
      local targetSuitChoice = U.askForChooseCardList(room, player, targetSuitNames, targetSuitCards, 1, 1,
        s15__qingzheng.name, "#s15__qingzheng-target-suit::", false, false)
      if #targetSuitChoice > 0 then
        targetDiscard = table.filter(to.player_cards[Player.Hand], function(id)
          return Fk:getCardById(id):getSuitString(true) == targetSuitChoice[1]
        end)
      end
    end

    -- Discard target's cards
    room:throwCard(targetDiscard, s15__qingzheng.name, to, player)

    -- Compare and deal damage if needed
    if #myDiscard > #targetDiscard and not to.dead then
      room:doIndicate(player.id, { to.id })
      room:damage {
        from = player,
        to = to,
        damage = 1,
        skillName = s15__qingzheng.name
      }
    end
  end,
})

return s15__qingzheng
